package aom.example.security.mapper;

import aom.example.security.domain.SysMenuEntity;
import aom.example.security.domain.SysRoleEntity;
import aom.example.security.domain.SysUserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:43
 * @since JDK 1.8
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUserEntity> {

    /**
     * 通过用户ID查询角色集合
     * @Author Sans
     * @CreateTime 2019/9/18 18:01
     * @Param  userId 用户ID
     * @Return List<SysRoleEntity> 角色名集合
     */
    @Select("SELECT sr.* FROM sys_role sr LEFT JOIN sys_user_role se ON se.role_id = sr.role_id WHERE se.user_id =#{userId}")
    List<SysRoleEntity> selectSysRoleByUserId(@Param("userId") Long userId);

    /**
     * 通过用户ID查询权限集合
     * @Author Sans
     * @CreateTime 2019/9/18 18:01
     * @Param  userId 用户ID
     * @Return List<SysMenuEntity> 角色名集合
     */
    @Select("SELECT DISTINCT m.* FROM sys_user_role ur LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id LEFT JOIN sys_menu m ON rm.menu_id = m.menu_id WHERE ur.user_id = #{userId}")
    List<SysMenuEntity> selectSysMenuByUserId(@Param("userId")Long userId);

    @Select("<script>" +
                "select * from (" +
                        "select id,a.user_id,a.role_id,username,`status`,realname,phone,address,c.role_name,role_no " +
                        " from  sys_user_role a LEFT JOIN sys_user b on a.user_id=b.user_id LEFT JOIN " +
                        " sys_role c ON a.role_id=c.role_id" +
                ") t " +
                "<where>" +
                    "<if test=\"keyword !=null \"> " +
                         "and username like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \">" +
                        "or realname like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \"> " +
                        " or role_name like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \"> " +
                        " or phone like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \"> " +
                        "  or address like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                "</where>" +
                        "limit #{page},#{size} " +
            "</script>")
    List<Map<String, Object>> selectUserInfoList(@Param("page")Integer page,@Param("size")Integer size,@Param("keyword")String keyword);


    @Select("<script>" +
                 "select count(1) from (" +
                        " select id,a.user_id,a.role_id,username,`status`,realname,phone," +
                        " address,c.role_name,role_no FROM  sys_user_role a LEFT JOIN " +
                        " sys_user b on a.user_id=b.user_id LEFT JOIN  sys_role c ON a.role_id=c.role_id " +
                    ") t " +
                "<where>" +
                    "<if test=\"keyword !=null \"> " +
                        " and username like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \">" +
                         " or realname like CONCAT('%',#{keyword},'%')" +
                    "</if> " +
                    "<if test=\"keyword !=null \"> " +
                        " or role_name like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \"> " +
                        " or phone like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                    "<if test=\"keyword !=null \"> " +
                        "  or address like CONCAT('%',#{keyword},'%')" +
                    "</if>" +
                "</where>" +
            "</script>")
    Integer selectCountUserInfoList(@Param("keyword")String keyword);




}
